package softtech.apps.sync.android.util;

import java.util.Comparator;

import softtech.apps.model.RowVoiceRecorded;

/**
 * Comparator providing a reasonable sort order for displaying row voice recorder listings to
 * a user.  
 */
public class RowVoiceRecorderListComparator implements Comparator<RowVoiceRecorded> {
    private final boolean isNameFirst;
	private final boolean isAscending;

    /**
     * Gets a comparator whose sort order places name before date.
     *
     * @param ascending whether the sort order should be ascending,
     *  vs. descending (reversed).
     * @param nameFirst is true if sort by name, else sort by time create
     */
    public static RowVoiceRecorderListComparator getNameFirst(boolean nameFirst, boolean ascending) {
        return new RowVoiceRecorderListComparator(nameFirst, ascending);
    }

    private RowVoiceRecorderListComparator(boolean nameFirst, boolean ascending) {
        isNameFirst = nameFirst;
        isAscending = ascending;
    }

    @Override
    public int compare(RowVoiceRecorded lhs, RowVoiceRecorded rhs) {
        int rawCmp = rawCompare(lhs, rhs);
        return isAscending ? rawCmp : -rawCmp;
    }

    int rawCompare(RowVoiceRecorded lhs, RowVoiceRecorded rhs) {
    	
    	if(isNameFirst){
    		return lhs.getmName().compareTo(rhs.getmName());
    	}else{
    		return lhs.getmTimeCreate().compareTo(rhs.getmTimeCreate());
    	}
    }

}